﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Skybot.Docs
{
    /// <summary>
    /// 词实体
    /// </summary>
    public class WordEntity
    {

        /// <summary>
        /// 创建新的实体
        /// </summary>
        public WordEntity()
        {

        }

        /// <summary>
        /// 用于初始化数据的字符串
        /// </summary>
        /// <param name="recordstr">反义词:,近意词:灾民|灾黎|难民,原词:哀鸿</param>
        public WordEntity(string recordstr)
        {
            string[] arr = recordstr.Split(',');
            Antonym = arr[0].Replace("反义词:", string.Empty).Replace("反意词:", string.Empty);

            NearSynonym = arr[1].Replace("近义词:", string.Empty).Replace("近意词:", string.Empty);

            //将汉字转换为&#编码的字符串
            NearSynonymUnicode = string.Join("|", GetUnicode(NearSynonym.Split('|')));
            OriginWord = arr[2].Replace("原词:", string.Empty);
            //如 果没有近意词则用原词
            if (string.IsNullOrEmpty(NearSynonym))
            {
                NearSynonymUnicode = GetUnicode(OriginWord);
            }
            arr = null;
        }


        /// <summary>
        /// 得到传入字符串的 &amp;#编码
        /// </summary>
        /// <param name="orginstr">原字符串</param>
        /// <returns></returns>
        public string GetUnicode(string orginstr)
        {
            return string.Join("", (from p in orginstr.ToArray()
                                    select "&#" + (int)p + ";").ToArray()
                          );
        }
        /// <summary>
        /// 得到传入字符串的 &amp;#编码 集合
        /// </summary>
        /// <param name="orginstr">原字符串集合</param>
        /// <returns>集合数组</returns>
        public IEnumerable<string> GetUnicode(IEnumerable<string> orginstr)
        {
            //用于保存结果的数据集合
            List<string> strArr = new List<string>();
            foreach (string item in orginstr)
            {
                strArr.Add(GetUnicode(item));
            }

            return strArr;
        }

        /// <summary>
        /// 原词
        /// </summary>
        private string _OriginWord = string.Empty;
        /// <summary>
        /// 原词
        /// </summary>
        public string OriginWord
        {
            get { return _OriginWord; }
            set { _OriginWord = value; }
        }

        /// <summary>
        /// 近义|分开
        /// </summary>
        private string _NearSynonym = string.Empty;
        /// <summary>
        /// 近义|分开
        /// </summary>
        public string NearSynonym
        {
            get { return _NearSynonym; }
            set { _NearSynonym = value; }
        }

        /// <summary>
        /// 近义 |分开的数据 &amp;# 表示
        /// </summary>
        private string _NearSynonymUnicode = string.Empty;
        /// <summary>
        /// 近义 |分开的数据 &amp;# 表示
        /// </summary>
        public string NearSynonymUnicode
        {
            get { return _NearSynonymUnicode; }
            set { _NearSynonymUnicode = value; }
        }

        /// <summary>
        /// 反义 |分开
        /// </summary>
        private string _Antonym = string.Empty;
        /// <summary>
        /// 反义 |分开
        /// </summary>
        public string Antonym
        {
            get { return _Antonym; }
            set { _Antonym = value; }
        }
        /// <summary>
        /// 获取当前类型的字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("反义词:{0},近意词:{1},原词:{2}", Antonym, NearSynonym, OriginWord);
        }

    }
}